
package com.bible.test.pad.activity;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

import com.bible.fire.R;
import com.bible.fire.api.TwoDimensionCode;
import com.bible.fire.local.AppContext;
import com.bible.fire.pad.activity.BaseActivity;
import com.bible.fire.service.BDLocationService;
import com.bible.fire.service.RestartAppService;
import com.bible.fire.util.ImageUtil;
import com.bible.fire.util.SystemConstant;
import com.google.zxing.BarcodeFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 简单的二维码
 * @author zhangyp
 *
 */
public class Simple2CodeActivity extends BaseActivity {
	
	private static final String TAG = Simple2CodeActivity.class.getSimpleName();
	
	private TextView resultTextView;
	private EditText editText;
	private ImageView imageView;
	private Button encodeButton;
	private Button decodeButton;
	private Button waterButton;
	private Button endThreadButton;
	private Button backDialogButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

	     super.onCreate(savedInstanceState);

	     setContentView(R.layout.activity_simaple2code);

	     encodeButton = (Button)findViewById(R.id.btn_encode );
	     
	     imageView = (ImageView)findViewById(R.id.img_2code );
	     
	     editText = (EditText)findViewById(R.id.et_encode);

	     encodeButton.setOnClickListener(new OnClickListener() {
	         @Override
	         public void onClick(View view) {	             
                 Log.i(TAG, "编辑框中的内容： " + editText.getText().toString());
                 if(editText.getText().toString().length()<1){
                     return;
                 }	                 
                 imageView.setImageBitmap( TwoDimensionCode.encodeToBitmap(editText.getText().toString(), BarcodeFormat.QR_CODE, 70, 70) );
	         }
	     });

	     resultTextView = (TextView)findViewById(R.id.tv_decode);
	     decodeButton = (Button)findViewById(R.id.btn_decode);
	     
	     decodeButton.setOnClickListener(new OnClickListener() {
	         @Override
	         public void onClick(View v) {
	        	 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
	             try {
	            	 resultTextView.setText( TwoDimensionCode.decodeBitmap(bitmap));
	                 }catch( Exception e){
	                     e.printStackTrace();
	                     Log.e(TAG, "", e);
	                 }
	            }
	     });
	     
	     waterButton = (Button)findViewById(R.id.btn_water2code);
	     waterButton.setOnClickListener(new OnClickListener() {
	         @Override
	         public void onClick(View v) {
	        	 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
	        	 FileOutputStream fos = null;
	             try {
	            	 Bitmap srcmap = BitmapFactory.decodeFile(SystemConstant.IMG_PATH_IN_SDCARD + "20130424_063944.jpg");
	            	 Bitmap newmap = ImageUtil.watermarkBitmap(srcmap, bitmap);
	            	 fos = new FileOutputStream( SystemConstant.IMG_PATH_IN_SDCARD + "2code.jpg" );
	            	 newmap.compress( Bitmap.CompressFormat.JPEG, 100, fos );
	            	 imageView.setImageBitmap( newmap );
	                 }catch( Exception e){
	                     e.printStackTrace();
	                     Log.e(TAG, "", e);
	                 }finally{
						   try {
							   fos.flush();
							   fos.close();
						   }catch(IOException e2){
							   Log.e( "ioexception", "file output error", e2 );
						   }
					 }
	            }
	     });
	     
	     endThreadButton = (Button)findViewById(R.id.btn_endThread);
	     endThreadButton.setOnClickListener(new OnClickListener() {
	         @Override
	         public void onClick(View v) {
	        	 Log.d(TAG, "throw NPE...");
	        	 throw new NullPointerException();
	         }
	     });
	     
	     backDialogButton = (Button)findViewById(R.id.btn_backDialog);
	     backDialogButton.setOnClickListener(new OnClickListener() {
	         @Override
	         public void onClick(View v) {
	        	 Log.d(TAG, "dialog from  server....");
	        	 Intent intent = new Intent(AppContext.getContext(), RestartAppService.class );
	        	 AppContext.getContext().startService( intent );
	         }
	     });
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
